]>
Commit | Line | Data |
---|---|---|
f8aec187 BB |
1 | using System; |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using Microsoft.Xna.Framework; | |
6 | using Microsoft.Xna.Framework.Content; | |
7 | using Microsoft.Xna.Framework.Graphics; | |
8 | ||
9 | namespace SuperPolarity | |
10 | { | |
11 | class Actor | |
12 | { | |
2af83e98 BB |
13 | protected Game game; |
14 | ||
f8aec187 BB |
15 | // Graphics / In-Game |
16 | protected Texture2D Texture; | |
17 | protected Vector2 Origin; | |
18 | public bool Active; | |
19 | ||
20 | // Physical Properties | |
2af83e98 | 21 | public Vector2 Position; |
f8aec187 BB |
22 | protected Vector2 Velocity; |
23 | protected Vector2 Acceleration; | |
24 | protected float Angle; | |
25 | ||
26 | // Constraints / Behavior | |
27 | protected float MaxVelocity; | |
28 | protected float AccelerationRate; | |
29 | ||
30 | public int Width | |
31 | { | |
32 | get { return Texture.Width; } | |
33 | } | |
34 | ||
35 | public int Height | |
36 | { | |
37 | get { return Texture.Height; } | |
38 | } | |
39 | ||
2af83e98 BB |
40 | public Actor(Game newGame) |
41 | { | |
42 | game = newGame; | |
43 | } | |
44 | ||
45 | public virtual void Initialize(Texture2D texture, Vector2 position) | |
f8aec187 BB |
46 | { |
47 | Texture = texture; | |
48 | Position = position; | |
49 | Active = true; | |
50 | ||
51 | Origin = new Vector2(Texture.Width / 2, Texture.Height / 2); | |
52 | Velocity = new Vector2(0, 0); | |
53 | Acceleration = new Vector2(0, 0); | |
54 | ||
55 | MaxVelocity = 5; | |
56 | AccelerationRate = 10; | |
57 | } | |
58 | ||
59 | public void AutoDeccelerate(GameTime gameTime) | |
60 | { | |
61 | if (Acceleration.X == 0 && Velocity.X > 0) | |
62 | { | |
63 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X) | |
64 | { | |
65 | Velocity.X = 0; | |
66 | Acceleration.X = 0; | |
67 | } | |
68 | else | |
69 | { | |
70 | Acceleration.X = -AccelerationRate; | |
71 | } | |
72 | } | |
73 | ||
74 | if (Acceleration.X == 0 && Velocity.X < 0) | |
75 | { | |
76 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X) | |
77 | { | |
78 | Velocity.X = 0; | |
79 | Acceleration.X = 0; | |
80 | } | |
81 | else | |
82 | { | |
83 | Acceleration.X = AccelerationRate; | |
84 | } | |
85 | } | |
86 | ||
87 | if (Acceleration.Y == 0 && Velocity.Y > 0) | |
88 | { | |
89 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y) | |
90 | { | |
91 | Velocity.Y = 0; | |
92 | Acceleration.Y = 0; | |
93 | } | |
94 | else | |
95 | { | |
96 | Acceleration.Y = -AccelerationRate; | |
97 | } | |
98 | } | |
99 | ||
100 | if (Acceleration.Y == 0 && Velocity.Y < 0) | |
101 | { | |
102 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y) | |
103 | { | |
104 | Velocity.Y = 0; | |
105 | Acceleration.Y = 0; | |
106 | } | |
107 | else | |
108 | { | |
109 | Acceleration.Y = AccelerationRate; | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | public virtual void Update(GameTime gameTime) | |
115 | { | |
116 | Move(gameTime); | |
117 | ChangeAngle(); | |
118 | } | |
119 | ||
120 | public void Move(GameTime gameTime) | |
121 | { | |
122 | AutoDeccelerate(gameTime); | |
123 | ||
124 | Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
125 | Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
126 | ||
127 | if (Velocity.X > MaxVelocity) | |
128 | { | |
129 | Velocity.X = MaxVelocity; | |
130 | } | |
131 | ||
132 | if (Velocity.X < -MaxVelocity) | |
133 | { | |
134 | Velocity.X = -MaxVelocity; | |
135 | } | |
136 | ||
137 | if (Velocity.Y > MaxVelocity) | |
138 | { | |
139 | Velocity.Y = MaxVelocity; | |
140 | } | |
141 | ||
142 | if (Velocity.Y < -MaxVelocity) | |
143 | { | |
144 | Velocity.Y = -MaxVelocity; | |
145 | } | |
146 | ||
147 | Position.X = Position.X + Velocity.X; | |
148 | Position.Y = Position.Y + Velocity.Y; | |
149 | } | |
150 | ||
151 | public void ChangeAngle() | |
152 | { | |
153 | Angle = (float)Math.Atan2(Velocity.Y, Velocity.X); | |
154 | } | |
155 | ||
156 | public virtual void Draw(SpriteBatch spriteBatch) | |
157 | { | |
158 | spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f); | |
159 | } | |
160 | } | |
161 | } |